﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebStickyNotes.Contracts;
using WebStickyNotes.Resources;

namespace WebStickyNotes.Helpers
{
    public static class PagingHelper
    {
        public static int GetPageNumber(int? page)
        {
            var pageNumber = 1;
            if (page != null)
            {
                pageNumber = (int)page;
            }
            return pageNumber;
        }

        public static string GetPageTitle(string title, int searchTerm, int pageNumber)
        {
            var pageTitle = new StringBuilder();
            pageTitle.Append(title);
            if (searchTerm > 0)
            {
                pageTitle.Append(" ");
                pageTitle.AppendFormat(Labels.FormatForSearchTitle, searchTerm);
            }
            if (pageNumber > 0)
            {
                pageTitle.Append(" ");
                pageTitle.AppendFormat(Labels.FormatForPageNumber, pageNumber);
            }
            return pageTitle.ToString();
        }

        public static string GetPageTitle(string title, string searchTerm, int pageNumber)
        {
            var pageTitle = new StringBuilder();
            pageTitle.Append(title);
            if (searchTerm.Length > 0)
            {
                pageTitle.AppendFormat(Labels.FormatForSearchTitle, searchTerm);
            }
            if (pageNumber > 0)
            {
                pageTitle.AppendFormat(Labels.FormatForPageNumber, pageNumber);
            }
            return pageTitle.ToString();
        }


        public static string ShowPageLinks(string address, string className, int currentPage, int pageSize, int totalRecords)
        {
            // Defaults to 5 links unless overridden.
            return ShowPageLinks(address, className, currentPage, pageSize, totalRecords, 5);
        }

        public static string ShowPageLinks(string address, string className, int currentPage, int pageSize, int totalRecords, int maximumPageLinks)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("<ul class=\"{0}\">", className);

            var pages = (int) Math.Ceiling((decimal)totalRecords/pageSize);

            if (maximumPageLinks < pages)
            {
                var linksEachSide = (int)Math.Floor((decimal)(maximumPageLinks - 2) / 2);
                var startPosition = (currentPage - linksEachSide) - 1;
                if (startPosition < 1)
                {
                    startPosition = 1;
                }

                var endPosition = startPosition + (maximumPageLinks - 2);
                if (endPosition >= pages)
                {
                    endPosition = (pages - 1);
                    startPosition = endPosition - (maximumPageLinks - 2);
                }

                var link = string.Format(address, 1);
                var selected = 1 == currentPage ? " class=\"selected\"" : string.Empty;
                stringBuilder.AppendFormat("<li><a href=\"{0}\"{2}>{1}</a></li>", link, 1, selected);

                stringBuilder.Append("<li>...</li>");

                for (var i = startPosition; i < endPosition; i++)
                {
                    link = string.Format(address, i + 1);
                    selected = i + 1 == currentPage ? " class=\"selected\"" : string.Empty;
                    stringBuilder.AppendFormat("<li><a href=\"{0}\"{2}>{1}</a></li>", link, i + 1, selected);
                }

                stringBuilder.Append("<li>...</li>");

                link = string.Format(address, pages);
                selected = pages == currentPage ? " class=\"selected\"" : string.Empty;
                stringBuilder.AppendFormat("<li><a href=\"{0}\"{2}>{1}</a></li>", link, pages, selected);
            }
            else
            {
                for (var i = 0; i < pages; i++)
                {
                    var link = string.Format(address, i + 1);
                    var selected = i + 1 == currentPage ? " class=\"selected\"" : string.Empty;
                    stringBuilder.AppendFormat("<li><a href=\"{0}\"{2}>{1}</a></li>", link, i + 1, selected);
                }
            }

            stringBuilder.Append("</ul>");

            return stringBuilder.ToString();
        }

        public static List<T> GetPageOfResults<T>(List<T> results, int pageSize, int pageNumber) where T : BaseEntity
         {
             var skip = pageSize * (pageNumber - 1);
             var page = results.Skip(skip).Take(pageSize).ToList();
             return page;
         }

        public static List<ExtendedTaskDetail> GetPageOfResults(List<ExtendedTaskDetail> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskDetail> GetPageOfResults(List<TaskDetail> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskAudit> GetPageOfResults(List<TaskAudit> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskProject> GetPageOfResults(List<TaskProject> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskStream> GetPageOfResults(List<TaskStream> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskPerson> GetPageOfResults(List<TaskPerson> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }

        public static List<TaskPriority> GetPageOfResults(List<TaskPriority> results, int pageSize, int pageNumber)
        {
            var skip = pageSize * (pageNumber - 1);
            var page = results.Skip(skip).Take(pageSize).ToList();
            return page;
        }
    }
}